package com.ourteam.system.dao.impl;

import com.ourteam.system.dao.ISysMessageDAO;
import com.ourteam.system.dao.SysMessage;
import com.ourteam.system.dao.SysMessageDAOQueryBean;

import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Template Name : DAO服务对象
 * Template Version : 1
 * Template Last Modfiy Date : Fri Nov 17 11:00:11 CST 2017
 * Auto Generate Date : 2018-07-31 15:13:40
  * Warning ! This file will be overwrite when regenerate !
  *
 */
public class AbstractSysMessageDAOJdbcImpl extends AbstractSpringDAO
    implements ISysMessageDAO {
    public static final String[] ENTITY_FIELDS = new String[] {
            Id, ParentId, SenderId, Title, Content, Priority, SendTime, Status,
            Remarks, SenderName, SenderAccount
        };
    public static final String[] ENTITY_PROPERTIES = new String[] {
            _DataObjectProperty.Id, _DataObjectProperty.ParentId,
            _DataObjectProperty.SenderId, _DataObjectProperty.Title,
            _DataObjectProperty.Content, _DataObjectProperty.Priority,
            _DataObjectProperty.SendTime, _DataObjectProperty.Status,
            _DataObjectProperty.Remarks, _DataObjectProperty.SenderName,
            _DataObjectProperty.SenderAccount
        };
    public static final String[] ENTITY_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {  };
    private static String relatedJoinSQL = null;
    private static String basicFilterSQL = null;
    private DataAccessObjectHelper<SysMessage, SysMessageDAOQueryBean> daoHelper;

    public AbstractSysMessageDAOJdbcImpl() {
        super();
        daoHelper = new DataAccessObjectHelper<SysMessage, SysMessageDAOQueryBean>(this);
    }

    protected String getEntityName() {
        return EntityName;
    }

    protected String[] getEntityFields() {
        return ENTITY_FIELDS;
    }

    protected String[] getEntityProperties() {
        return ENTITY_PROPERTIES;
    }

    protected String[] getEntityPKFields() {
        return ENTITY_PK_FIELDS;
    }

    protected String[] getEntityAutoGenPKFields() {
        return ENTITY_AUTO_GEN_PK_FIELDS;
    }

    protected String[] getEntityUniqueFields() {
        return ENTITY_UNIQUE_FIELDS;
    }

    protected String getBaiscQueryFilter() {
        if (basicFilterSQL == null) {
            StringBuffer basicFilterSQLbuffer = new StringBuffer();

            basicFilterSQL = basicFilterSQLbuffer.toString();
        }

        return basicFilterSQL;
    }

    protected String getRelatedJoinSQL() {
        if (relatedJoinSQL == null) {
            StringBuffer relatedJoinSQLBuffer = new StringBuffer();

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.system.dao.ISysUserDAO.EntityName)
                                .append(" SYS_USER ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.system.dao.ISysUserDAO.Id,
                    com.ourteam.system.dao.ISysUserDAO.EntityName + ".",
                    "SYS_USER."));
            relatedJoinSQLBuffer.append(" = ").append(SenderId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQL = relatedJoinSQLBuffer.toString();
        }

        return relatedJoinSQL;
    }

    /**
         * Check Data Unique
         * @param aSysMessage
         * @throws Exception
         */
    public boolean checkIsUnique(SysMessage aSysMessage)
        throws Exception {
        if (aSysMessage == null) {
            return true;
        }

        List valueList = new ArrayList();

        List pkValueList = new ArrayList();

        pkValueList.add(aSysMessage.getId());

        return isUnique(valueList.toArray(new Object[valueList.size()]),
            pkValueList.toArray(new Object[pkValueList.size()]));
    }

    /**
     * Insert Data
     * @param aSysMessage
     * @throws Exception
     */
    public void insert(SysMessage aSysMessage) throws Exception {
        super.doInsert(aSysMessage);
    }

    /**
         * Batch Insert Datas
         * @param aSysMessage
         * @throws Exception
         */
    public void batchInsert(SysMessage[] aSysMessages)
        throws Exception {
        super.doBatchInsert(aSysMessages);
    }

    /**
         * Delete Data
         * @param  final long id         *
         * @throws Exception
         */
    public void delete(final long id) throws Exception {
        super.doDeleteById(new Object[] { new Long(id) });
    }

    /**
     * Delete Datas
     * @param queryBean
     * @throws Exception
     */
    public void delete(SysMessageDAOQueryBean queryBean)
        throws Exception {
        super.doDeleteByQuery(queryBean);
    }

    public void delete(SysMessage aSysMessage) throws Exception {
        delete(aSysMessage.getId());
    }

    /**
     * Update Data Selective
     * @param aSysMessage
     * @throws Exception
     */
    public void update(SysMessage aSysMessage) throws Exception {
        if (aSysMessage.getId() == 0) {
            throw new Exception(
                "Can not update object data whith out primary key value");
        }

        super.doUpdate(aSysMessage);

        SysMessage tempObj = findById(aSysMessage.getId());

        if (tempObj != null) {
            PropertyUtils.copyProperties(aSysMessage, tempObj);
        }
    }

    /**
     * Batch Update Datas
     * @param aSysMessages[]
     * @throws Exception
     */
    public void batchUpdate(SysMessage[] aSysMessages)
        throws Exception {
        super.doBatchUpdate(aSysMessages);
    }

    /**
         * Update Data By Condictions
         * @param aSysMessage
         * @param aValueCriteria
         * @param aTransaction
         * @throws Exception
         */
    public void update(SysMessage aSysMessage, SysMessageDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aSysMessage, aQueryBean);
    }

    /**
     * Update Data By Condictions
     * @param aQueryBean
     * @throws Exception
     */
    public void update(SysMessageDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aQueryBean);
    }

    protected void checkNullValue(SysMessage aSysMessage)
        throws Exception {
        if (aSysMessage.getTitle() == null) {
            throw new Exception("The SysMessage.title value is not allow NULL");
        }

        if (aSysMessage.getContent() == null) {
            throw new Exception(
                "The SysMessage.content value is not allow NULL");
        }

        if (aSysMessage.getPriority() == null) {
            throw new Exception(
                "The SysMessage.priority value is not allow NULL");
        }

        if (aSysMessage.getSendTime() == null) {
            throw new Exception(
                "The SysMessage.sendTime value is not allow NULL");
        }

        if (aSysMessage.getStatus() == null) {
            throw new Exception("The SysMessage.status value is not allow NULL");
        }

        if (aSysMessage.getRemarks() == null) {
            throw new Exception(
                "The SysMessage.remarks value is not allow NULL");
        }

        if (aSysMessage.getSenderName() == null) {
            throw new Exception(
                "The SysMessage.senderName value is not allow NULL");
        }

        if (aSysMessage.getSenderAccount() == null) {
            throw new Exception(
                "The SysMessage.senderAccount value is not allow NULL");
        }
    }

    /**
     * Get Data By ID
     * @param  final long id         * @return  ${businessObject.name}
     * @throws Exception
     */
    public SysMessage findById(final long id) throws Exception {
        SysMessageDAOQueryBean queryBean = new SysMessageDAOQueryBean();

        SysMessageDAOQueryBean.Criteria criteria = queryBean.createCriteria();

        criteria.andIdEqualTo(id);

        //queryBean.setDistinct(true);
        SysMessage[] SysMessages = query(queryBean);

        if (SysMessages.length == 0) {
            return null;
        } else {
            return SysMessages[0];
        }
    }

    /**
         * Query Data
         * @param queryBean
         * @return SysMessage[]
         * @throws Exception
         */
    public SysMessage[] query(SysMessageDAOQueryBean queryBean)
        throws Exception {
        List result = super.doQuery(queryBean, new JdbcSysMessageRowMapper());

        return (SysMessage[]) result.toArray(new SysMessage[result.size()]);
    }

    /**
     * Query ${businessObject.name} Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySelective(
        SysMessageDAOQueryBean queryBean) throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addAllSelectProperties();
        }

        return super.doQuerySelective(queryBean);
    }

    /**
     * Query Data Count
     * @param queryBean
     * @return int
     * @throws Exception
     */
    public int queryCount(SysMessageDAOQueryBean queryBean)
        throws Exception {
        return super.doQueryCount(queryBean);
    }

    public void synchronize(SysMessage[] aSysMessages,
        SysMessageDAOQueryBean queryBean) throws Exception {
        daoHelper.synchronize(aSysMessages, queryBean);
    }

    public void synchronize(SysMessage[] aSysMessages,
        SysMessageDAOQueryBean queryBean,
        net.dao.IDataAccessInterface.ISynchronizeCallback<SysMessage> callBack)
        throws Exception {
        daoHelper.synchronize(aSysMessages, queryBean, callBack);
    }

    public static class JdbcSysMessageRowMapper implements RowMapper<SysMessage> {
        public SysMessage mapRow(ResultSet resultSet, int arg1)
            throws SQLException {
            SysMessage object = new SysMessage();

            int columnIndex = 1;

            int __dataType;

            try {
                Object id = null;

                id = resultSet.getObject(columnIndex++);

                object.setIdNull(resultSet.getBoolean(columnIndex++));

                if (id != null) {
                    setBeanValue(object, _DataObjectProperty.Id, id);
                }

                Object parentId = null;

                parentId = resultSet.getObject(columnIndex++);

                object.setParentIdNull(resultSet.getBoolean(columnIndex++));

                if (parentId != null) {
                    setBeanValue(object, _DataObjectProperty.ParentId, parentId);
                }

                Object senderId = null;

                senderId = resultSet.getObject(columnIndex++);

                object.setSenderIdNull(resultSet.getBoolean(columnIndex++));

                if (senderId != null) {
                    setBeanValue(object, _DataObjectProperty.SenderId, senderId);
                }

                Object title = null;

                title = resultSet.getObject(columnIndex++);

                object.setTitleNull(resultSet.getBoolean(columnIndex++));

                if (title != null) {
                    setBeanValue(object, _DataObjectProperty.Title, title);
                }

                Object content = null;

                content = resultSet.getObject(columnIndex++);

                object.setContentNull(resultSet.getBoolean(columnIndex++));

                if (content != null) {
                    setBeanValue(object, _DataObjectProperty.Content, content);
                }

                Object priority = null;

                priority = resultSet.getObject(columnIndex++);

                object.setPriorityNull(resultSet.getBoolean(columnIndex++));

                if (priority != null) {
                    setBeanValue(object, _DataObjectProperty.Priority, priority);
                }

                Object sendTime = null;

                __dataType = resultSet.getMetaData().getColumnType(columnIndex);

                if ((__dataType == java.sql.Types.DATE) ||
                        (__dataType == java.sql.Types.TIME) ||
                        (__dataType == java.sql.Types.TIMESTAMP)) {
                    sendTime = resultSet.getTimestamp(columnIndex++);
                } else {
                    sendTime = resultSet.getObject(columnIndex++);
                }

                object.setSendTimeNull(resultSet.getBoolean(columnIndex++));

                if (sendTime != null) {
                    setBeanValue(object, _DataObjectProperty.SendTime, sendTime);
                }

                Object status = null;

                status = resultSet.getObject(columnIndex++);

                object.setStatusNull(resultSet.getBoolean(columnIndex++));

                if (status != null) {
                    setBeanValue(object, _DataObjectProperty.Status, status);
                }

                Object remarks = null;

                remarks = resultSet.getObject(columnIndex++);

                object.setRemarksNull(resultSet.getBoolean(columnIndex++));

                if (remarks != null) {
                    setBeanValue(object, _DataObjectProperty.Remarks, remarks);
                }

                Object senderName = null;

                senderName = resultSet.getObject(columnIndex++);

                object.setSenderNameNull(resultSet.getBoolean(columnIndex++));

                if (senderName != null) {
                    setBeanValue(object, _DataObjectProperty.SenderName,
                        senderName);
                }

                Object senderAccount = null;

                senderAccount = resultSet.getObject(columnIndex++);

                object.setSenderAccountNull(resultSet.getBoolean(columnIndex++));

                if (senderAccount != null) {
                    setBeanValue(object, _DataObjectProperty.SenderAccount,
                        senderAccount);
                }

                return object;
            } catch (Exception e) {
                throw new SQLException(e.getMessage());
            }
        }
    }
}
